<template>
  <div class="parking-record-container">
    <el-card>
      <template #header>
        <div class="card-header">
          <h2>停车记录</h2>
          <el-button type="primary" @click="fetchParkingRecords">刷新</el-button>
        </div>
      </template>
      <el-table :data="parkingRecords" style="width: 100%">
        <el-table-column prop="id" label="记录ID" width="80" />
        <el-table-column prop="plateNumber" label="车牌号" width="120" />
        <el-table-column prop="parkingId" label="停车场ID" width="100" />
        <el-table-column prop="vehicleType" label="车辆类型" width="100">
          <template #default="scope">
            <el-tag :type="scope.row.vehicleType === 1 ? 'success' : 'warning'">
              {{ scope.row.vehicleType === 1 ? '月租车' : '临时车' }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="status" label="状态" width="100">
          <template #default="scope">
            <el-tag :type="scope.row.status === 0 ? 'success' : 'info'">
              {{ scope.row.status === 0 ? '在场' : '已离场' }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="startTime" label="入场时间" width="180">
          <template #default="scope">
            {{ formatTime(scope.row.startTime) }}
          </template>
        </el-table-column>
        <el-table-column prop="endTime" label="出场时间" width="180">
          <template #default="scope">
            {{ formatTime(scope.row.endTime) }}
          </template>
        </el-table-column>
        <el-table-column prop="orderId" label="订单ID" width="120" />
      </el-table>
    </el-card>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { ElMessage } from 'element-plus'
import { getAllParkingRecords } from '../api/parkingRecord'

const parkingRecords = ref([])

const fetchParkingRecords = async () => {
  try {
    const res = await getAllParkingRecords()
    parkingRecords.value = res.data
  } catch (e) {
    ElMessage.error('获取停车记录失败')
  }
}

const formatTime = (time) => {
  if (!time) return ''
  return new Date(time).toLocaleString()
}

onMounted(() => {
  fetchParkingRecords()
})
</script>

<style scoped>
.parking-record-container {
  padding: 20px;
}
.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
</style> 